package com.mapbased.wfw;

import java.io.IOException;
import java.util.*;

import org.dom4j.Element;
import com.mapbased.wfw.bind.ComponentBinding;
import com.mapbased.wfw.handler.DefaultPageHandler;

/**
 *
 *
 * <p>Title:Page </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class Page
    extends Component
{

    private Component[] headables;
    private PageHandler pageHandler=new DefaultPageHandler();
    final public Element parseXml(Element ele,PageLoader.LoadContext pl)
    {
        return ele;
    }

    final public void renderHeader(RenderContext rc,StringBuffer sb,Map m)
    {

       renderBinding(rc,this.headSegments,sb);

        //  sb.append(this.headString);

    }

    public void render(RenderContext context)
        throws IOException
    {
        if(this.headables != null)
        {
            StringBuffer sb=new StringBuffer();
            Map ids=new HashMap(); // ArrayList();
            for(int i=0;i < this.headables.length;i++)
            {
                this.headables[i].renderHeader(context,sb,ids);
            }

            context.getRequestMap().put(Constants.Generated_Head,
                                        sb.toString());

        }
        this.defalutRenderBehavior(context);

    }

    public void setPageHandler(PageHandler pageHandler)
    {
        this.pageHandler=pageHandler;
    }

    public PageHandler getPageHandler()
    {
        pageHandler.lastVisitTime=System.currentTimeMillis();
        return pageHandler;
    }

    public Component[] getHeadables()
    {
        return headables;
    }



    /**
     * setSegments
     *
     * @param segments Object[]
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void setSegments(Object[] segments)
    {
        this.segments=segments;
        List l=new ArrayList();
        readyForComponentBinding(segments,null);
        findHeadables(segments,l);
        if(this.isHeadable())
        {
            l.add(this);
        }
        this.headables=(Component[])l.toArray(new Component[l.size()]);
    }

    private void readyForComponentBinding(Object[] segs,Component parent)
    {
        for(int i=0;i < segs.length;i++)
        {
            if(segs[i] instanceof Component)
            {
                Component c=(Component)segs[i];
                readyForComponentBinding(c.getSegments(),c);

            }
            else if(segs[i] instanceof ComponentBinding)
            {
                ((ComponentBinding)segs[i]).setComponent(parent);
            }
        }

    }

    private void findHeadables(Object[] segs,List l)
    {
        for(int i=0;i < segs.length;i++)
        {
            if(segs[i] instanceof Component)
            {
                Component c=(Component)segs[i];
                findHeadables(c.getSegments(),l);
                if(c.isHeadable())
                {
                    l.add(c);
                }
            }
        }
    }

    Validating[] findValidators()
    {
        List l=new ArrayList(2);
        this.findValidators(this.segments,l);
        return(Validating[])l.toArray(new Validating[l.size()]);
    }

    /**
     * find all validators in segments
     * @param segs Object[]
     * @param l List
     */
    private void findValidators(Object[] segs,List l)
    {
        for(int i=0;i < segs.length;i++)
        {
            if(segs[i] instanceof Component)
            {
                Component c=(Component)segs[i];
                findValidators(c.getSegments(),l);
                if(c instanceof Validating)
                {
                    l.add(c);
                }
            }
        }
    }

}
